package plotter;

import java.util.ArrayList;
import java.util.List;

import processor.Result;
import util.AppUtil;

public class GraphPlotterImpl implements GraphPlotter{
	
	
	private List<GraphPlotterInfo> lstGPObjects = new ArrayList<GraphPlotterInfo>();
	
	private double count;
	
	private int totalCriticality;
	
	public GraphPlotterImpl(List<GraphPlotterInfo> lstObjects, double count, int totalCriticality){
		
		this.lstGPObjects = lstObjects;
		this.count = count;
		this.totalCriticality = totalCriticality;		
	}
	
	public List<GraphPlotterInfo> getGraphPoints(){
		
		for(GraphPlotterInfo gp : lstGPObjects){
			
			double startValue = 0;
			double thresholdValue = 0;
			double newCount;
			
			List<Point> points = new ArrayList<Point>();
									
			if(gp.getParameter().getParamType().equalsIgnoreCase("Integer") || 
					gp.getParameter().getParamType().equalsIgnoreCase("Double")){
				
				startValue = Double.parseDouble(gp.getCurrentValue());
				thresholdValue = Double.parseDouble(gp.getCondition().getValue());
				
			}
			

			if(gp.getRiskFactor().getResult() == Result.TRUE){
				newCount = count - gp.getRiskFactor().getDeviationFromThreshold() - gp.getRiskFactor().getCriticality();
			}else{				
				newCount = count;
						
			}
				
			
								
			if(gp.getCondition().getOperator().equalsIgnoreCase(">=") || 
					gp.getCondition().getOperator().equalsIgnoreCase(">")){
				
				if(gp.getRiskFactor().getResult() == Result.NOT_AVAILABLE)
					startValue = thresholdValue - 5;	
				
				for(double i=startValue; i<startValue+20; i = i+2){
					double count1 = newCount + ((i-thresholdValue)*1.0/thresholdValue) + gp.getCondition().getCriticality();
					double probability = count1/totalCriticality;
					Point p = new Point(i,AppUtil.formatDecimal(probability));
					System.out.println(p);
					points.add(p);
				}
				
				gp.setGraphPoints(points);
			}
			
			if(gp.getCondition().getOperator().equalsIgnoreCase("<=") 
					|| gp.getCondition().getOperator().equalsIgnoreCase("<")){
				
				if(gp.getRiskFactor().getResult() == Result.NOT_AVAILABLE)
					startValue = thresholdValue +5;	
				    double endValue = startValue-15;
				for(double i=endValue; i<=startValue; i=i+2){
					double count1 = newCount + ((thresholdValue - i)*1.0/thresholdValue) + gp.getCondition().getCriticality();
					double probability = count1/totalCriticality;
					Point p = new Point(i,AppUtil.formatDecimal(probability));
					System.out.println(p);
					points.add(p);
				}
				
				gp.setGraphPoints(points);
			}
		}
		return lstGPObjects;
	}

}
